package com.clz.hyperit.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.clz.hyperit.dao.UserDao;
import com.clz.hyperit.dao.UserDetailDao;
import com.clz.hyperit.entity.User;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {

	private static Logger logger = LoggerFactory
			.getLogger(UserServiceImpl.class);
	
	@Resource
	private UserDao userDao;
	@Resource
	private UserDetailDao userDetailDao;
	public User findByAccount(String account){
		return userDao.findByAccount(account);
	}
	public Integer saveRegister(User user){
		return userDao.save(user);
	}
	public UserDetail saveUserDetail(Integer userId){
		UserDetail ud = new UserDetail();
		ud.setName("用户"+userId.toString());
		ud.setUser_id(userId);
		ud.setSex(false);
		ud.setExp(0);
		ud.setTotal_sign(0);
		ud.setHb(0);
		ud.setFans(0);
		ud.setArticle(0);
		ud.setAccept(0);
		ud.setNote(0);
		ud.setHead("/headpath/default.jpg");
		ud.setContinous_sign(0);
		ud.setPosition_id(1);
		ud.setCity("");
		ud.setSignature("");
		ud.setSkill("");
		ud.setHobby("");
		ud.setAddress("");
		ud.setStatus(1);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		//获取日期前一天
		 Date date=new Date();  
	        Calendar calendar = Calendar.getInstance();  
	        calendar.setTime(date);  
	        calendar.add(Calendar.DAY_OF_MONTH, -1);  
	        date = calendar.getTime();  
		ud.setSigntime(df.format(date));
		UserDetail ud_2 = userDetailDao.getByUserId(userId);
		if(ud_2!=null){
			return ud_2;
		}
		else{
		return 	userDetailDao.get(userDetailDao.save(ud));
			//return userDetailDao.getByUserId(userId);
		}
	}
	
	public UserDetail getUserDetailByUserID(Integer id){
		return userDetailDao.getByUserId(id);
	}
	public void updatePwd(User user) {
		userDao.updateWithNotNullProperties(user);
		
	}
	public User getById(Integer user_id) {
		return userDao.get(user_id);
	}
}
